/* Copyright Thanh-Trung Pham, JAIST, 2012 */

package jp.ac.jaist.computation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Thanh-Trung Pham
 */
public class RMSensitivityAnalysisData {

    private static final String RM_SPACE = " ";
    private String m_strSAFigureCaption;
    private String m_strSAGraphTitle;
    private String m_strSAAxisXTitle;
    private String m_strSAAxisYTitle;
    private List<Double> m_oAxisXValues;
    private Map<String, List> m_oAllAxisYValues;

    /**
     * The constructor.
     * @param saFigureCaption the sensitivity analysis figure caption.
     * @param saGraphTitle the sensitivity analysis graph title.
     * @param saAxisXTitle the sensitivity analysis axis X title.
     * @param saAxisYTitle the sensitivity analysis axis Y title.
     */
    public RMSensitivityAnalysisData(String saFigureCaption, String saGraphTitle, String saAxisXTitle, String saAxisYTitle) {
        m_strSAFigureCaption = saFigureCaption;
        m_strSAGraphTitle = saGraphTitle;
        m_strSAAxisXTitle = saAxisXTitle;
        m_strSAAxisYTitle = saAxisYTitle;

        m_oAxisXValues = new ArrayList<Double>();
        m_oAllAxisYValues = new HashMap<String, List>();
    }

    /**
     * Get the SA figure caption.
     * @return the SA figure caption.
     */
    public String getSAFigureCaption(){
        return m_strSAFigureCaption;
    }

    /**
     * Add a value into axis X value collection.
     * @param value the value to be added.
     */
    public void addAxisXValue(double value) {
        m_oAxisXValues.add(value);
    }

    /**
     * Add a value into axis Y value collection.
     * @param fullProvidedServiceName the full provided service name.
     * @param legendLabel the legend.
     * @param value the value to be added.
     */
    public void addAxisYValue(String fullProvidedServiceName, String legendLabel, double value) {
        String strFullLegend = getFullLegend(fullProvidedServiceName, legendLabel);

        if (m_oAllAxisYValues.containsKey(strFullLegend)) {
            List<Double> oAxisYValues = m_oAllAxisYValues.get(strFullLegend);
            oAxisYValues.add(value);
        } else {
            List<Double> oAxisYValues = new ArrayList<Double>();
            oAxisYValues.add(value);
            m_oAllAxisYValues.put(strFullLegend, oAxisYValues);
        }
    }

    /**
     * Get the full legend from a full provided service name and a legend label.
     * @param fullProvidedServiceName a full provided service name.
     * @param legendLabel a legend label.
     * @return the full legend.
     */
    private String getFullLegend(String fullProvidedServiceName, String legendLabel) {
        return fullProvidedServiceName + RM_SPACE + legendLabel;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder oStringBuilder = new StringBuilder();

        oStringBuilder.append("Figure caption:");
        oStringBuilder.append("\t");
        oStringBuilder.append(m_strSAFigureCaption);
        oStringBuilder.append("\n");

        oStringBuilder.append("Graph title:");
        oStringBuilder.append("\t");
        oStringBuilder.append(m_strSAGraphTitle);
        oStringBuilder.append("\n");

        oStringBuilder.append("Axis X title:");
        oStringBuilder.append("\t");
        oStringBuilder.append(m_strSAAxisXTitle);
        oStringBuilder.append("\n");

        oStringBuilder.append("Axis Y title:");
        oStringBuilder.append("\t");
        oStringBuilder.append(m_strSAAxisYTitle);
        oStringBuilder.append("\n");

        oStringBuilder.append("Axis X values:");
        oStringBuilder.append("\t");
        oStringBuilder.append("\t");

        for (int i = 0; i < m_oAxisXValues.size(); i++) {
            oStringBuilder.append(m_oAxisXValues.get(i));

            if (i != m_oAxisXValues.size() - 1) {
                oStringBuilder.append("\t");
            }
        }

        oStringBuilder.append("\n");

        oStringBuilder.append("Axis Y values:");

        Iterator<String> oKeyIterator = m_oAllAxisYValues.keySet().iterator();

        while (oKeyIterator.hasNext()) {
            String strFullLegend = oKeyIterator.next();

            oStringBuilder.append("\t");
            oStringBuilder.append(strFullLegend);
            oStringBuilder.append("\t");
            List<Double> oAxisYValues = m_oAllAxisYValues.get(strFullLegend);

            for (int i = 0; i < oAxisYValues.size(); i++) {
                oStringBuilder.append(oAxisYValues.get(i));

                if (i != oAxisYValues.size() - 1) {
                    oStringBuilder.append("\t");
                }
            }

            if (oKeyIterator.hasNext()) {
                oStringBuilder.append("\n");
            }
        }

        return oStringBuilder.toString();
    }

    /**
     * Get the GNUPLOT data block.
     * @param userInterfaceName the
     * @param dataFileName the data file name.
     * @param dataIndex the data index.
     * @param gnuplotPLT the GNUPLOT PLT.
     * @return the GNUPLOT data block.
     */
    public String getGNUPLOTDataBlock(String dataFileName, int dataIndex, StringBuilder gnuplotPLT) {
        gnuplotPLT.append("set title \"").append(m_strSAFigureCaption).append("\\n").append(m_strSAGraphTitle).append("\"\n");
        gnuplotPLT.append("set xlabel \"").append(m_strSAAxisXTitle).append("\"\n");
        gnuplotPLT.append("set ylabel \"").append(m_strSAAxisYTitle).append("\"\n");
        gnuplotPLT.append("plot");

        StringBuilder oStringBuilder = new StringBuilder();
        oStringBuilder.append("# ").append(m_strSAFigureCaption).append("\n");
        oStringBuilder.append("# ").append(m_strSAGraphTitle).append("\n");

        int iColumnCount = 0;
        oStringBuilder.append("# Column ").append(String.valueOf(++iColumnCount)).append(": ").append(m_strSAAxisXTitle).append("\n");

        int iNumberOfValues = m_oAxisXValues.size();
        String[] strRowsOfValues = new String[iNumberOfValues];
        for (int i = 0; i < iNumberOfValues; i++) {
            strRowsOfValues[i] = String.valueOf(m_oAxisXValues.get(i));
        }


        Iterator<String> oKeyIterator = m_oAllAxisYValues.keySet().iterator();
        while (oKeyIterator.hasNext()) {
            String strFullLegend = oKeyIterator.next();
            oStringBuilder.append("# Column ").append(String.valueOf(++iColumnCount)).append(": ").append(m_strSAAxisYTitle).append(" - Modifying ").append(strFullLegend).append("\n");

            List<Double> oAxisYValues = m_oAllAxisYValues.get(strFullLegend);
            for (int i = 0; i < oAxisYValues.size(); i++) {
                strRowsOfValues[i] += "\t" + oAxisYValues.get(i);
            }

            gnuplotPLT.append(" \"../").append(dataFileName).append("\" index ").append(dataIndex).append(" using 1:").append(String.valueOf(iColumnCount))
                    .append(" with linespoints title \"").append(strFullLegend).append("\"");

            if(oKeyIterator.hasNext()){
                gnuplotPLT.append(",");
            }
        }

        for (int i = 0; i < iNumberOfValues; i++) {
            oStringBuilder.append(strRowsOfValues[i]).append("\n");
        }

        return oStringBuilder.toString();
    }
}
